import re
import time

def get_result(regex_str):
    "获取符合年月日规则的字符"
    with open("pi.txt", "r") as f:
        pi_text = f.read()
    pattern = re.compile(regex_str)
    total_list = re.finditer(pattern, pi_text)
    result = []
    for item in total_list:
        posi = item.start()
        value = item.group()
        result.append((posi,int(value)))
    return result

def compare_time(first_time, second_time):
    return abs(first_time - second_time)

def birth_time_relate(birth_time,result):
    "获取生日最近的日期"
    init_num = compare_time(result[0][1], birth_time)
    tag = result[0]
    for k in result[1:]:
        if compare_time(k[1],birth_time) < init_num:
            tag = k
            init_num = compare_time(k[1],birth_time)
    return tag

if __name__ == '__main__':
    regex_str = '19[8-9][0-9]((0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])(29|30)|(0[13578]|1[02])31)|(19(8[048]|9[26]))0229'
    result = get_result(regex_str)
    print('1980-2000符合年月日规则的字符:',result)
#     1980-2000符合年月日规则的字符: [(8354, 19910403), (13863, 19950728), (25582, 19990814), (27201, 19810705), (30873, 19921122), (36604, 19930509), (37836, 19840906), (41306, 19891025), (42486, 19890605), (50646, 19870521), (70927, 19960520), (76251, 19890518), (96837, 19800301), (107851, 19861207), (119706, 19900511), (124109, 19801121), (128512, 19820730), (136825, 19860410), (153617, 19890315), (158249, 19960413), (160657, 19881211), (168977, 19910626), (180592, 19940403), (191561, 19950725), (193722, 19881104), (218074, 19920816), (260042, 19951017), (267407, 19981018), (275255, 19900517), (277265, 19950917), (279893, 19980627), (280254, 19870213), (288362, 19830825), (307927, 19930608), (323579, 19900402), (372017, 19910209), (388084, 19910309), (394595, 19950221), (395117, 19980910), (399669, 19800602), (417571, 19830505), (417807, 19920823), (421460, 19930113), (422819, 19820113), (454250, 19900529), (468338, 19871104), (493840, 19830608), (508938, 19840829), (512634, 19970516), (516933, 19830729), (519320, 19870814), (530547, 19960202), (534258, 19820302), (543302, 19881214), (553186, 19930624), (561159, 19950111), (561467, 19830928), (569678, 19920416), (571521, 19870228), (608599, 19950310), (610554, 19960927), (611791, 19891225), (634002, 19920810), (637441, 19800618), (643816, 19820301), (651064, 19990908), (661148, 19921218), (684839, 19880728), (699699, 19800718), (718621, 19841204), (724004, 19990220), (729909, 19811126), (730879, 19991202), (747929, 19931117), (779817, 19971019), (809189, 19950531), (821458, 19920115), (834221, 19930711), (877501, 19870623), (880372, 19930610), (888081, 19860829), (889470, 19990430), (896138, 19950731), (901728, 19930730), (938284, 19881009), (942363, 19990131), (952884, 19940110), (956788, 19960517), 
# (981988, 19870325), (986871, 19880308), (987486, 19831002)]
    print('跟本人生日19930415比较接近的日期：',birth_time_relate(19930415,result))
    # 跟本人生日19930415比较接近的日期： (36604, 19930509)

    regex_str2 = r"19[6-9][0-9]|200[0-9]|201[0-9]|202[0-1]"
    result2 = get_result(regex_str2)
    print('1960--2021 符合年月日规则的字符:',result2)
    

